home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / newsgroups / misc.19990725-20000114 / 000428_news@columbia.edu _Sat Jan 8 16:23:51 2000.msg < prev    next >
Internet Message Format  |  2020-01-01  |  7KB

  1. Return-Path: <news@columbia.edu>
  2. Received: from newsmaster.cc.columbia.edu (newsmaster.cc.columbia.edu [128.59.59.30])
  3.     by watsun.cc.columbia.edu (8.8.5/8.8.5) with ESMTP id QAA20340
  4.     for <kermit.misc@watsun.cc.columbia.edu>; Sat, 8 Jan 2000 16:23:50 -0500 (EST)
  5. Received: (from news@localhost)
  6.     by newsmaster.cc.columbia.edu (8.8.5/8.8.5) id PAA06440
  7.     for kermit.misc@watsun.cc.columbia.edu; Sat, 8 Jan 2000 15:56:10 -0500 (EST)
  8. X-Authentication-Warning: newsmaster.cc.columbia.edu: news set sender to <news> using -f
  9. From: fdc@watsun.cc.columbia.edu (Frank da Cruz)
  10. Subject: Case Study #2: Kerbang Scripts
  11. Date: 8 Jan 2000 20:56:09 GMT
  12. Organization: Columbia University
  13. Message-ID: <858899$695$1@newsmaster.cc.columbia.edu>
  14. To: kermit.misc@columbia.edu
  15.  
  16.  
  17. Today's topic is Kerbang scripts.  This discussion is oriented mainly to
  18. UNIX users, and was alluded to in yesterday's posting.  Here is a fuller
  19. explanation.
  20.  
  21. A nice feature of UNIX is the ability to run shell scripts just if they
  22. were compiled binary programs or built-in shell commands, just by typing
  23. their names and, optionally, including command-line arguments.  For
  24. example, suppose that as a common task you need to move some files to the
  25. /usr/local/etc/ directory; your shell script might look like this:
  26.  
  27.   for i in $*; do
  28.       echo $i =\> /usr/local/etc/$i
  29.       mv $i /usr/local/etc/$i
  30.   done
  31.  
  32. If you saved this script as a file called (say) "move" in a directory
  33. that is in your PATH, and gave it execute permission:
  34.  
  35.   chmod +x move
  36.  
  37. then you could type commands such as:
  38.  
  39.   move foo.bar
  40.   move *.log
  41.   move file1 file2 file3 ...
  42.  
  43. at the shell prompt, and the script would move all the files whose names
  44. were given on the command line (wildcards are automatically expanded by
  45. the shell into a list of matching files).
  46.  
  47. The shell gives us various mechanisms to refer to command-line arguments
  48. within a script: $* is replaced by all the arguments, $# is replaced by
  49. the number of arguments, $1 is replaced by the first argument, $2 by the
  50. second, and so forth.  There is also a "shift" command that can be used
  51. (e.g.) to access arguments beyond $9.
  52.  
  53. Now suppose you need some features in your script that were only available
  54. in a particular shell, which might not be the default shell.  Most UNIXes
  55. let you include a "special comment" as the first line of a script, which
  56. starts with the characters "#!" and then gives the full pathname of the
  57. shell which is to execute the script, e.g.:
  58.  
  59.   #!/bin/ksh
  60.  
  61. This is sometimes called the "shebang" line ("she" for shell and "bang" is
  62. Unix slang for the exclamation mark).  It is not indented; the shebang
  63. line must be on the left margin (indentation is used here to set examples
  64. off from text).
  65.  
  66. Happily, this convention can be applied to any program at all, Perl for
  67. example, so Perl scripts generally have execute permission and a first
  68. line like:
  69.  
  70.   #!/usr/local/bin/perl
  71.  
  72. This lets us run Perl scripts just as if they were shell scripts or, for
  73. that matter, precompiled programs or built-in shell commands.
  74.  
  75. C-Kermit 7.0 extends this idea to Kermit scripts.  In this case the
  76. shebang-line contains the full path of the Kermit program, followed by
  77. a space and then a plus (+) sign:
  78.  
  79.   #!/usr/local/bin/kermit +
  80.  
  81. and the last line in the script is usually EXIT, to make C-Kermit exit
  82. when the script is finished (the plus sign means "treat command-line
  83. arguments as arguments for the script, not arguments for Kermit").  We
  84. call these Kerbang scripts.
  85.  
  86. Here is a simple Kerbang script that prints its arguments:
  87.  
  88.   #/usr/local/bin/kermit +
  89.   echo Hello from \%0
  90.   for \%i 0 \v(argc)-1 1 {
  91.       echo \%i. "\&_[\%i]"
  92.   }
  93.   exit 0
  94.  
  95. In Kermit the number of command line arguments (including the name of
  96. the script itself) is in the \v(argc) variable.  The "argument vector"
  97. is a special array, \&_[], having \v(argc) elements, 0 through \v(argc)-1.
  98. The loop above prints each argument.
  99.  
  100. Save this file as (say) "showargs", then give it execute permission and
  101. run it:
  102.  
  103.   $ chmod +x showargs
  104.   $ ./showargs one "this is two" three
  105.  
  106. The script displays its arguments:
  107.  
  108.   Hello from /usr/olga/showargs
  109.   0. "/usr/olga/showargs"
  110.   1. "one"
  111.   2. "this is two"
  112.   3. "three"
  113.  
  114. Notice that shell quoting rules, not Kermit ones, apply to the command-
  115. line arguments since you are, indeed, typing this command to the shell,
  116. which parses the arguments and passes them along to Kermit.
  117.  
  118. Kermit scripts give you the same ways to access the command line arguments
  119. as shell scripts do, but with different syntax:
  120.  
  121.   Shell   Kermit        Description
  122.    $#      \v(argc)-1     The number of command-line arguments
  123.    $*      \%*            A string containing all the command-line arguments
  124.    $0      \%0            The name of the script
  125.    $1      \%1            The first command-line argument
  126.    $2      \%2            The second command-line argument
  127.    $3      \%3            The third command-line argument
  128.    ...     ...            ...
  129.    $9      \%9            The ninth command-line argument
  130.    shift   shift          Shift the command-line arguments
  131.    (???)  \&_[]           The entire command-line argument vector,
  132.                           even if there are more than 10 elements.
  133.    $?      \v(status)     Completion status of previous command.
  134.  
  135. Of course you can put any commands at all into a Kerbang script.  It can
  136. read and write files, make connections, transfer files, anything that
  137. Kermit can do -- because it *is* Kermit.  And of course, Kerbang scripts
  138. can also be executed from the Kermit prompt (or from another script) with
  139. a TAKE command; the Kerbang line is ignored since it starts with "#",
  140. which is a comment introducer to Kermit just as it is to the UNIX shell.
  141. In VMS and other non-UNIX platforms, the Kerbang line has no effect.
  142.  
  143. An especially handy use for Kerbang scripts is to have the C-Kermit
  144. initialization file itself be one.  Since the standard initialization file
  145. is rather long and time-consuming to execute (setting up your services
  146. directory, etc), it is often overkill if you want to start Kermit just to
  147. transfer a file.  Of course there is a command-line switche to suppress
  148. initialization-file execution but another approach is to "run" the
  149. initialization file when you want its features (notably the services
  150. directory), and run C-Kermit directly when you don't.  A setup like this
  151. requires that (a) the C-Kermit initialization file is configured as a
  152. Kerbang script (has #!/path../kermit as first line), has execute
  153. permission, and is in your PATH; and (b) that you don't have a .kermrc
  154. file in your login directory.
  155.  
  156. Almost all of this is new to C-Kermit 7.0, and there are lots more
  157. wrinkles to it.  Of course Kerbang scripts can be used in VMS and other
  158. non-Unix platforms, but the methods for invoking them are different.  For
  159. details, see the ckermit2.txt file, Sections 7.5 and 7.19.  To see lots of
  160. sample Kerbang scripts, many of them quite practical, visit the C-Kermit
  161. Script Library at:
  162.  
  163.   http://www.columbia.edu/kermit/ckscripts.html
  164.  
  165. - Frank